import { createRouter, createWebHistory} from 'vue-router'
import store from '@/store/index'
import {Notify} from 'vant'
const routes = [
  {
    path:"/home",
    component:()=>import('../views/home/index')
  },
  {
    path: '/login',
    component:()=>import('@/views/login/index')
  },
  {
    path:'/register',
    component:()=>import('@/views/profile/Register/index')
  },
  {
    path:'/',
    redirect:'/home',
    meta:{title:'图书兄弟-首页'}
  },
  {
    path: '/category',
    component:()=>import('../views/category/index'),
    meta:{title:'图书兄弟-分类'}
  },
  {
    path:'/detail',
    component:()=>import('../views/detail/index'),
    meta:{title:'图书兄弟-详情'}
  },
  {
  path: '/profile',
   component:()=>import('../views/profile/index'),
    meta:{title:'图书兄弟-我的',isAuthRequired:true}
  },
  {
    path: '/shopCart',
    component:()=>import('../views/shopCart/index'),
    meta:{title:'图书兄弟-购物车',isAuthRequired:true}
  },
  {
    path:'/address',
    component:()=>import('@/views/profile/address/address')
  },
  {
    path:'/addressEdit',
    component:()=>import('@/views/profile/address/addressEdit')
  },
  {
    path:'/createOrder',
    component:()=>import('@/views/order/createOrder/index')
  },
  {
    path:'/orderDetail',
    component:()=>import('@/views/order/orderDetail/index')
  },
  {
    name:'404',
    path:'/404',
    component:()=>import('@/views/404/index')
  },

  {
    path: '/:pathMatch(.*)',
    redirect: '/404'
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  scrollBehavior (to, from, savedPosition) {
    // return 期望滚动到哪个的位置
    return {top:0}
  },
  routes
})
router.beforeEach((to,from,next)=>{
  document.title=to.meta.title||'首页'
  if(to.meta.isAuthRequired && store.state.user.userIsLogin===false){
    console.log('123')
    Notify('你还没有登录')
    return next('/login')
  }else{
    return next()
  }
})
export default router
